interface IApi {
    appId: boolean;
    description: string;
    id: unknown;
    method: string;
    name: string;
    tag: string;
    url: string;
}

/**
 * @description: 格式化api数据，用于树形组件使用
 * @param {IApi} apis
 * @param {*} disabled
 * @return {*}
 */
export const formatTree = (apis: IApi[], disabled = false) => {
    const tags = [...new Set(apis.map(item => item.tag))];
    const tree = apis.map((api, i) => {
        api.id = tags.length + i;
        return { ...api, disabled, id: api.id };
    });
    return tags.map((tag, index) => ({
        id: index,
        name: tag,
        tag,
        disabled,
        children: tree.filter(i => i.tag === tag)
    }));
};

/**
 * @description: 将项目列表转换为属性结构
 * @param {object[]} list
 */
export function formatProjects(list: any[], supProjectId: any = 0) {
    // 所有顶级项目
    const projects = list.filter(p => p.supProjectId === supProjectId);
    projects.forEach(p => {
        p.children = formatProjects(list, p.id);
    });
    return projects;
}
